package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.Painter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;

public class CompDecision extends Componente {
	final static int MaxCharacterPerLine = 10;
	final static int HorizontalMargin = 30;

	private ArrayList<String> TextLine = new ArrayList<String>();
	private String longestString = "Caso";
	private Point textPoint;
	private int textLineSpan;

	public CompDecision() {
		_centerPoint = new Point(0, 0);
		textPoint = new Point();
		textLineSpan = 0;

		_connectionPoints = new Point[4];
		_connectionPoints[0] = new Point((int) (_centerPoint.x + (_width / 2)),
				_centerPoint.y);
		_connectionPoints[1] = new Point(_centerPoint.x,
				(int) (_centerPoint.y - (_height / 2)));
		_connectionPoints[2] = new Point((int) (_centerPoint.x - (_width / 2)),
				_centerPoint.y);
		_connectionPoints[3] = new Point(_centerPoint.x,
				(int) (_centerPoint.y + (_height / 2)));
	}

	@Override
	public void setTitulo(String titulo) {
		Titulo = titulo;
		TextLine = DividirTitulo(titulo);
		int aux;
		for (String value : TextLine) {
			aux = value.length();
			if (aux > longestString.length()) {
				longestString = value;
			}
		}
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		int posX = _centerPoint.x + offSetX;
		int posY = _centerPoint.y + offSetY;
		int textWidht;
		Paint borde = Painter.getInstancia().getBorderPaint(_currentState);

		// Dibujamos el texto
		for (int i = 0; i < TextLine.size(); i++) {
			textWidht = (int) Painter.getInstancia()
					.getTextPaint(false, globalScale)
					.measureText(TextLine.get(i));
			c.drawText(TextLine.get(i), ((posX * globalScale) - textWidht / 2),
					((textPoint.y + offSetY)
							- (textLineSpan * (TextLine.size() - i - 1)) - 2)
							* globalScale,
					Painter.getInstancia().getTextPaint(false, globalScale));
		}

		// Dibujamos el contorno
		c.drawLine((_connectionPoints[0].x + offSetX) * globalScale,
				(_connectionPoints[0].y + offSetY) * globalScale,
				(_connectionPoints[1].x + offSetX) * globalScale,
				(_connectionPoints[1].y + offSetY) * globalScale, borde);
		c.drawLine((_connectionPoints[1].x + offSetX) * globalScale,
				(_connectionPoints[1].y + offSetY) * globalScale,
				(_connectionPoints[2].x + offSetX) * globalScale,
				(_connectionPoints[2].y + offSetY) * globalScale, borde);
		c.drawLine((_connectionPoints[2].x + offSetX) * globalScale,
				(_connectionPoints[2].y + offSetY) * globalScale,
				(_connectionPoints[3].x + offSetX) * globalScale,
				(_connectionPoints[3].y + offSetY) * globalScale, borde);
		c.drawLine((_connectionPoints[3].x + offSetX) * globalScale,
				(_connectionPoints[3].y + offSetY) * globalScale,
				(_connectionPoints[0].x + offSetX) * globalScale,
				(_connectionPoints[0].y + offSetY) * globalScale, borde);

		// Point z = calcularPuntoExterno(-0.5);
		// c.drawCircle(z.x, z.y, 10,
		// Painter.getInstancia().getArrowPaint(_currentState, true));

		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}

		// c.drawCircle(posX*globalScale, posY*globalScale, 3,
		// Painter.getInstancia().getBorderPaint(_currentState));
	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if ((touch.x <= Anchor.x + _width) && (touch.x >= Anchor.x)) {
			if ((touch.y <= Anchor.y + _height) && (touch.y >= Anchor.y)) {
				respuesta = true;
			}
		}
		return respuesta;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		_connectionPoints[0] = new Point((int) (_centerPoint.x + (_width / 2)),
				_centerPoint.y);
		_connectionPoints[1] = new Point(_centerPoint.x,
				(int) (_centerPoint.y - (_height / 2)));
		_connectionPoints[2] = new Point((int) (_centerPoint.x - (_width / 2)),
				_centerPoint.y);
		_connectionPoints[3] = new Point(_centerPoint.x,
				(int) (_centerPoint.y + (_height / 2)));
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsBegining) {
				value.CalcularConexiones(this, VistaDiagrama.getVista()
						.getDiagrama().getComponenteById(value._childId));
			}
		}
		if (VistaDiagrama.getVista() != null) {
			for (CompConector value : _conectorsEnd) {
				value.CalcularConexiones(VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(value._fatherId), this);
			}
		}
	}

	@Override
	public void actualizarPuntoCentral() {
		Rect mBounds = new Rect();
		int TotalTextHeight = 0;
		for (int i = 0; i < TextLine.size(); i++) {
			Painter.getInstancia().getTextPaint(false, 1)
					.getTextBounds(TextLine.get(i), 0, 2, mBounds);
			TotalTextHeight += (int) ((float) mBounds.height() * 1.2);
		}
		_width = Painter.getInstancia().getTextPaint(false, 1)
				.measureText(longestString)
				+ HorizontalMargin * 2;
		_height = _width;
		_centerPoint.x = (int) (Anchor.x + (_width / 2));
		_centerPoint.y = (int) (Anchor.y + (_height / 2));
		textPoint.x = (int) (_centerPoint.x - (Painter.getInstancia()
				.getTextPaint(false, 1).measureText(longestString) / 2));
		textPoint.y = _centerPoint.y + (TotalTextHeight / 2);
		if (TextLine.size() > 0) {
			textLineSpan = TotalTextHeight / TextLine.size();
		}
	}

	@Override
	public String generarDOM() {
		String DOM = "\t<Componente Tipo=\"Decision\" Titulo=\"" + Titulo
				+ "\" ID=\"" + ID + "\" AnchorX=\"" + Anchor.x
				+ "\" AnchorY=\"" + Anchor.y + "\">\n";
		DOM = DOM + "\t</Componente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		// TODO Auto-generated method stub

	}

	@Override
	public Point requestConnectionPoint(CompConector connector, int mode,
			Point otherCompCenter) {
		float distMin = Float.MAX_VALUE;
		float dX = 0;
		float dY = 0;
		float disActual;
		int CP_MasCercano = 0; // Punto de conexion con menor distancia
		for (int i = 0; i < _connectionPoints.length; i++) {
			dX = _connectionPoints[i].x - otherCompCenter.x;
			dY = _connectionPoints[i].y - otherCompCenter.y;
			disActual = (float) Math.sqrt(dX * dX + dY * dY);
			if (disActual < distMin) {
				distMin = disActual;
				CP_MasCercano = i;
			}
		}
		return _connectionPoints[CP_MasCercano];

	}

	public ArrayList<String> DividirTitulo(String titulo) {
		ArrayList<String> respuesta = new ArrayList<String>();
		String[] arreglo = titulo.split(" ");
		String linea = "";
		int acumulatedChar = 0;
		for (int i = 0; i < arreglo.length; i++) {
			acumulatedChar += arreglo[i].length();
			linea += arreglo[i] + " ";
			if (acumulatedChar >= MaxCharacterPerLine) {
				respuesta.add(linea);
				linea = "";
				acumulatedChar = 0;
			}
		}
		if (acumulatedChar > 0) {
			respuesta.add(linea);
		}
		return respuesta;
	}

	@Override
	public void mover(int distX, int distY) {
		Anchor.x += distX;
		Anchor.y += distY;
		actualizarPuntoCentral();
		actualizarAchorsPoints();

	}

	@Override
	public String getTipo() {
		return "Decision";
	}
}
